package com.cenntro.common.util;

import org.apache.commons.lang3.StringUtils;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
 
 
/**
 * 汉字转化为拼音的工具类
 *
 */
@Slf4j
public class PinyinTool {
    HanyuPinyinOutputFormat format = null;
    public static enum Type {
        UPPERCASE,              //全部大写
        LOWERCASE,              //全部小写
        FIRSTUPPER              //首字母大写
    }
 
    public PinyinTool(){
        format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    }
 
    public String toPinYin(String str){
        return toPinYin(str, "", Type.UPPERCASE);
    }
 
    public String toPinYin(String str,String spera) {
        return toPinYin(str, spera, Type.UPPERCASE);
    }
 
    /**
     * 将str转换成拼音，如果不是汉字或者没有对应的拼音，则不作转换
     * 如： 明天 转换成 MINGTIAN
     * @param str：要转化的汉字
     * @param spera：转化结果的分割符
     * @return
     * @throws BadHanyuPinyinOutputFormatCombination
     */
    public String toPinYin(String str, String spera, Type type)  {
        if(str == null || str.trim().length()==0)
            return "";
        if(type == Type.UPPERCASE)
            format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        else
            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
 
        String py = "";
        String temp = "";
        String[] t = null;;
        for(int i=0;i<str.length();i++){
            char c = str.charAt(i);
            if((int)c <= 128)
                py += c;
            else{
                try {
					t = PinyinHelper.toHanyuPinyinStringArray(c, format);
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					log.info("拼音转换成汉字异常： {}", str, e);
				}
                if(t == null)
                    py += c;
                else{
                    temp = t[0];
                    if(type == Type.FIRSTUPPER)
                        temp = t[0].toUpperCase().charAt(0)+temp.substring(1);
                    py += temp+(i==str.length()-1?"":spera);
                }
            }
        }
        return py.trim();
    }
    
    /**
     * 汉字转字母 取首字母
     * @param str
     * @param spera
     * @param type
     * @return
     */
    public String toPinyinFirstWord(String str, String spera, Type type){
    	String firstWord = "";
    	String pinyin = null;
    	spera = StringUtils.isBlank(spera) ? "#" : spera;
    	pinyin = this.toPinYin(str, spera, type);
    	if (StringUtils.isNotBlank(pinyin)){
    		String[] $temp = pinyin.split(spera);
    		for(String p : $temp){
    			firstWord += p.charAt(0);
    		}
    	}
    	return firstWord;
    }
    
	public static void main(String[] args) throws Exception{
    		PinyinTool tool = new PinyinTool();
    		System.out.println("高阳王====" + tool.toPinYin("高阳王", "", Type.LOWERCASE));
    		System.out.println("高阳王====" + tool.toPinyinFirstWord("三菱电机", "", Type.LOWERCASE));

	}
}
